package com.cuit.controller;

import com.cuit.domain.File;
import com.cuit.service.impl.RecycleServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/recycle")
public class RecycleController {

    @Resource
    private RecycleServiceImpl service;

    /**
     * 删除单个文件，从数据库删除
     * @param fileId
     * @return
     */
    @RequestMapping("/realDeleteFile/{fileId}")
    public int realDeleteFile(@PathVariable int fileId){
        return service.deleteFile(fileId);
    }

    /**
     * 批量删除文件，从数据库删除
     * @param filesId
     * @return
     */
    @RequestMapping("/realDeleteFiles/{filesId}")
    public int realDeleteFiles(@PathVariable int[] filesId){
        for(int i=0;i<filesId.length;i++){
            service.deleteFile(filesId[i]);
        }
        return 1;
    }

    /**
     * 将单个文件放回回收站，不是真正的删除
     * @param fileId
     * @return
     */
    @RequestMapping("/fakeDeleteFile/{fileId}")
    public int fakeDeleteFile(@PathVariable int fileId){
        return service.fakeDeleteFile(fileId,1);
    }

    /**
     * 将文件批量放回回收站，不是真正的删除
     * @param filesId
     * @return
     */
    @RequestMapping("/fakeDeleteFiles/{filesId}")
    public int fakeDeleteFiles(@PathVariable int[] filesId){
        for(int i=0;i<filesId.length;i++){
            service.fakeDeleteFile(filesId[i],1);
        }
        return 1;
    }

    /**
     * 将单个文件从回收站拿回来
     * @param fileId
     * @return
     */
    @RequestMapping("/cancelFile/{fileId}")
    public int cancelFile(@PathVariable int fileId){
        return service.fakeDeleteFile(fileId,0);
    }

    /**
     * 将文件批量从回收站拿回
     * @param filesId
     * @return
     */
    @RequestMapping("/cancelFiles/{filesId}")
    public int cancelFiles(int[] filesId){
        for(int i=0;i<filesId.length;i++){
            service.fakeDeleteFile(filesId[i],0);
        }
        return 1;
    }

    @RequestMapping("/showGarbage/{userId}")
    @ResponseBody
    public List<File> showGarbage(@PathVariable int userId, Model model) {
        List<File> files = service.viewGarbage(1);
        model.addAttribute("files", files);
        return files;
    }
}
